package org.xtense.util;

public class StringUtil {

	public static boolean isEmpty(String str) {
		return str == null || str.trim().equals("");
	}
	
	public static String append(Object[] st) {
		StringBuilder sb = new StringBuilder();
		for(Object s : st) {
			sb.append(s.toString());
		}
		return sb.toString();
	}
	
	/**
	 * Given a standard xpath expression, change delimiter "/" to "#/" and "//"
	 * to "#%/". The goal is to make query in database correct.
	 * 
	 * @param pathExpr
	 *            standard xpath exrpession.
	 * @return string after changed delimiter.
	 */
	public static String changeDelimiter(String pathExpr) {
		if (pathExpr.length() == 0) {
			return "/#";
		}
		StringBuilder sb = new StringBuilder(pathExpr);
		// we should support "/" but not "//"
		if (pathExpr.length() > 1) {
			sb.append('#');
		}
		int in = sb.indexOf("/");
		while (in > -1) {
			sb.insert(in, '#');
			in = sb.indexOf("/", in + 2);
		}
		
		in = sb.indexOf("#/#/");
		while (in > -1) {
			sb.replace(in, in + 4, "#%/");
			in = sb.indexOf("#/#/");
		}

		return sb.toString();
	}
}
